// 用户数据模块
const TOKEN = uni.getStorageSync("token") || "";
const OPENID = uni.getStorageSync("openId") || "";
const USER_INFO = uni.getStorageSync("userInfo") || {};
const ISNORMALX = uni.getStorageSync("isNormalX") || true;
const ADDRESS = uni.getStorageSync("address") || [{name:'全国找',id:0}];
const LASTADDRESS = uni.getStorageSync("lastAddress") || [{name:'全国找',id: 0}];


export const state = {
	token: '',
	hasLogin: false, // 是否登录
	user:{}, // 用户信息
	// 长辈模式默认为true 开启则为false
	isNormalX: true
}
// actions 通常跟api接口打交道
export const actions = {
	async setUserData({
		state,
		commit
	}, data) {
		commit('setStateAttr', {
			key: 'userInfo',
			val: data
		})
		uni.setStorageSync('userInfo', data);
	},
  async setUserToken({state,commit,dispatch}, info) {
    const {token,data} = info
		commit('setStateAttr', {
			key: 'token',
			val: token
		})
		uni.setStorageSync('token', token);

    dispatch('setUserData',data)
	},
	// 登录过期 重新登录
	reLogin({
		commit,dispatch
	}, info) {
		dispatch('setUserToken','')
		dispatch('setUserData','')
	
	}
}

export const mutations = {
	//更新state数据
	setStateAttr(state, param) {
		if (param instanceof Array) {
			for (let item of param) {
				state[item.key] = item.val;
			}
		} else {
			state[param.key] = param.val;
		}
	}
}

export const getters = {
	// 用户是否登录
	hasLogin: state => {
		if (state.token) {
			return true;
		} else {
			return false
		}
	}
}

